import Vue from 'vue'

Vue.directive('drag', {
  inserted(el) {
    el.onmousedown = (e) => {
      document.querySelectorAll('.block-wrap').forEach(node => node.style.zIndex = 2);
      e.target.parentNode.style.zIndex = 3;
      let innerX = e.clientX - el.parentNode.offsetLeft;
      let innerY = e.clientY - el.parentNode.offsetTop;
      document.onmousemove = (e) => {
        el.parentNode.style.left = e.clientX - innerX + 'px';
        el.parentNode.style.top = e.clientY - innerY + 'px';
      }
      document.onmouseup = () => {
        document.onmousemove = null;
        document.onmouseup = null;
      }
      // 防止黏连
      return false;
    }
  }
})
